package casa.depesos.app.com.frag;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.support.toast.ToastUtil;

import casa.depesos.app.com.R;
import casa.depesos.app.com.activity.BaseActivity;
import casa.depesos.app.com.activity.LoginActivity;
import casa.depesos.app.com.databinding.FragmentNewPwdBinding;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.GetCodeBean;
import casa.depesos.app.com.http.req.ForgetPwdReq;
import casa.depesos.app.com.utils.Constant;
import casa.depesos.app.com.utils.Report;
import casa.depesos.app.com.utils.Utils;

public class NewPwdFragment extends BaseFragment {

    protected FragmentNewPwdBinding binding;
    protected String phone, code;
    protected int type;

    /**
     * 0:设置
     * 1：修改
     * 2：遗忘
     */
    public NewPwdFragment setParam(String phone, String code, int type) {
        this.phone = phone;
        this.code = code;
        this.type = type;
        return this;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        if (type == 2) {
            Report.show("ACTION_FF_006");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentNewPwdBinding.inflate(inflater, container, false);
        binding.top.top.setPadding(0, Utils.getStatusBarHeight(getActivity()), 0, 0);
        binding.top.title.setText("Establecer nueva contraseña");
        binding.top.back.setOnClickListener(v -> ((BaseActivity) getActivity()).removeFragment(this));

        binding.eye.setSelected(false);
        binding.eye.setOnClickListener(v -> {
            boolean bo = !binding.eye.isSelected();
            binding.eye.setSelected(bo);
            if (bo) {
                binding.eye.setImageResource(R.mipmap.eye_open);
                binding.pwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            } else {
                binding.eye.setImageResource(R.mipmap.eye_close);
                binding.pwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            }
        });

        binding.eye2.setSelected(false);
        binding.eye2.setOnClickListener(v -> {
            boolean bo = !binding.eye2.isSelected();
            binding.eye2.setSelected(bo);
            if (bo) {
                binding.eye2.setImageResource(R.mipmap.eye_open);
                binding.pwd2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            } else {
                binding.eye2.setImageResource(R.mipmap.eye_close);
                binding.pwd2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            }
        });
        Utils.setEditTextInputSpace(binding.pwd, 20);
        Utils.setEditTextInputSpace(binding.pwd2, 20);
        binding.pwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                binding.err0.setVisibility(View.GONE);
                check();
            }
        });
        binding.pwd2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                binding.err1.setVisibility(View.GONE);
                check();
            }
        });
        binding.next.setOnClickListener(v -> next());
        return binding.getRoot();
    }

    protected boolean check() {
        String p0 = binding.pwd.getText().toString().trim();
        String p1 = binding.pwd2.getText().toString().trim();
        int i = p0.length();
        int j = p1.length();
        if (i >= 6 && j >= 6) {
            binding.next.setAlpha(1f);
            return true;
        }
        binding.next.setAlpha(0.6f);
        return false;
    }

    protected boolean isEmpty(EditText et, TextView tv) {
        String s = et.getText().toString().trim();
        if (s.isEmpty()) {
            tv.setVisibility(View.VISIBLE);
            tv.setText("Ingrese su contraseña por favor");
            return true;
        }
        return false;
    }

    protected boolean isValid(EditText et, TextView tv) {
        String s = et.getText().toString().trim();
        if (s.length() < 6) {
            tv.setVisibility(View.VISIBLE);
            tv.setText("Ingrese una contraseña de al menos 6 caracteres");
            return false;
        }
        return true;
    }

    protected boolean isValid() {
        String p0 = binding.pwd.getText().toString().trim();
        String p1 = binding.pwd2.getText().toString().trim();
        boolean valid = true;
        binding.err0.setVisibility(View.GONE);
        binding.err1.setVisibility(View.GONE);
        boolean bo1 = isEmpty(binding.pwd, binding.err0);
        boolean bo2 = isEmpty(binding.pwd2, binding.err1);
        if (!bo1 && !bo2) {
            bo1 = isValid(binding.pwd, binding.err0);
            bo2 = isValid(binding.pwd2, binding.err1);
            if (bo1 && bo2) {
                if (!TextUtils.equals(p0, p1)) {
                    valid = false;
                    binding.err0.setVisibility(View.GONE);
                    binding.err1.setVisibility(View.VISIBLE);
                    binding.err1.setText("Las contraseñas que ingresó no coinciden. Verifique y vuelva a ingresarlas");
                }
            } else {
                valid = false;
            }
        } else {
            valid = false;
        }
        return valid;
    }

    protected void next() {
        if (!isValid()) {
            return;
        }
        send();
    }

    protected void send() {
        showLoading();
        if (type == 2) {
            Report.click("ACTION_FF_007");
        }
        String p0 = binding.pwd.getText().toString().trim();
        new ForgetPwdReq(phone, p0, code).req(new ReqCallback<GetCodeBean>() {
            @Override
            public void callback(boolean success, GetCodeBean bean) {
                hideLoading();
                if (Constant.white || success && bean.isValid()) {
                    ToastUtil.showShortToast(getActivity(), "Contraseña modificada exitosamente");
                    if (getActivity() instanceof LoginActivity) {
                        ((LoginActivity) getActivity()).changeWay(1, "");
                    } else {
                        getActivity().finish();
                    }
                }
            }
        });
    }
}
